package cn.cms.filter;

import cn.cms.constants.Menu;
import cn.cms.model.SysUser;
import cn.cms.util.SessionUtil;
import cn.cms.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by leon on 2017/2/13.
 */
public class AdminRoleFilter extends HttpServlet implements Filter {

    private static Logger logger = LoggerFactory.getLogger(AdminRoleFilter.class);

    /**
     * filter过滤器配置信息
     */
    protected FilterConfig filterConfig = null;

    private String notCheckURLRegx = "";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
        String notCheckURLRegxStr = filterConfig.getInitParameter("notCheckURLRegx");
        if (notCheckURLRegxStr != null) {
            String[] ss = notCheckURLRegxStr.split(";");
            notCheckURLRegx += ".*(";
            for (int i = 0; i < ss.length; i++) {
                notCheckURLRegx += ss[i];
                if (i < ss.length - 1) {
                    notCheckURLRegx += "|";
                }
            }
            notCheckURLRegx += ")";
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getRequestURI();
        // 删除工程根目录部分
        String contextPath = request.getContextPath();
        uri = uri.replaceFirst(contextPath, "");

        SysUser sysUser = SessionUtil.getUserSession(request);

        if (
                uri.matches(notCheckURLRegx) ||
                        uri.equals(Menu.USER_LOGIN) ||
                        uri.equals(Menu.USER_LOGOUT)) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        if (StringUtil.isEmptyOrNull(sysUser)) {
            if (RequestMethod.GET.toString().equals(request.getMethod())) {
                request.getRequestDispatcher("/sysuser/login").forward(request, response);
                return;
            } else {
                response.setHeader("Error-Json", "{\"code\":\"121004\"}");
                throw new ServletException("121004");
            }
        } else {
            if (uri.equals(Menu.INDEX) || uri.equals(Menu.MANGO_STAT)) {

            } else if (RequestMethod.GET.toString().equals(request.getMethod())) {
                request.getRequestDispatcher("/sysuser/login").forward(request, response);
                return;
            }
        }

        filterChain.doFilter(servletRequest, servletResponse);
        return;
    }
}
